package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDTO;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags="03.评论模块")
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    //自动装配
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 1.发表评论功能
     */
    @ApiOperation("发表评论功能")
    @PostMapping("insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        System.out.println("commentDTO = " + commentDTO);
        /*
            1.校验登录状态
              * 未登录状态：return 2;
              * 已登录状态：插入数据[INSERT接口],return 1;
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return 2;
        }
        //插入数据
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());

        commentMapper.insert(comment);

        return 1;
    }

    /**
     * 2.获取评论列表功能
     */
    @ApiOperation("获取评论列表功能")
    @GetMapping("selectByWeiboId")
    public List<CommentVO> selectByWeiboId(int id){
        return commentMapper.selectByWeiboId(id);
    }
}
